package cn.tedu.hm.controller;

import cn.tedu.hm.pojo.Admin;
import cn.tedu.hm.pojo.dto.AdminAddNewDTO;
import cn.tedu.hm.pojo.dto.AdminLoginInfoDTO;
import cn.tedu.hm.pojo.dto.AdminUpdateDTO;
import cn.tedu.hm.pojo.vo.AdminListItemVO;
import cn.tedu.hm.pojo.vo.AdminStandardVO;
import cn.tedu.hm.security.LoginPrincipal;
import cn.tedu.hm.service.IAdminService;
import cn.tedu.hm.wep.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

/**
 * 说明:
 *
 * @ author: WeiZhen
 * @date: 2023/3/6
 **/
@RestController
@Slf4j
@RequestMapping("/admins")
@Api(tags = "01. 管理员管理模块")
public class AdminController {
    @Autowired
    IAdminService adminService;
    // http://localhost:9081/admins/9527/enable
    @PostMapping("/{id:[0-9]+}/enable")
    @ApiOperation("启用管理员")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "管理员ID", required = true, dataType = "long")
    })
    public JsonResult<Void> setEnable(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id) {
        log.debug("开始处理【启用管理员】的请求，参数：{}", id);
        adminService.setEnable(id);
        return JsonResult.ok();
    }

    // http://localhost:9081/admins/9527/disable
    @PostMapping("/{id:[0-9]+}/disable")
    @ApiOperation("禁用管理员")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "管理员ID", required = true, dataType = "long")
    })
    public JsonResult<Void> setDisable(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id) {
        log.debug("开始处理【禁用管理员】的请求，参数：{}", id);
        adminService.setDisable(id);
        return JsonResult.ok();
    }

    @ApiOperation("登录")
    @PostMapping("/login")
    public JsonResult<String> login(AdminLoginInfoDTO adminLoginInfoDTO){
        log.debug("开始处理【登录】的请求，参数：{}", adminLoginInfoDTO);
        String jwt = adminService.login(adminLoginInfoDTO);
        return JsonResult.ok(jwt);
    }

    //@PreAuthorize("hasAuthority('/ams/admin/add-new')")
    @ApiOperation("添加管理员")
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(AdminAddNewDTO adminAddNewDTO){
        log.debug("开始处理【添加管理员】的请求，参数：{}", adminAddNewDTO);

        adminService.addNew(adminAddNewDTO);
        return JsonResult.ok();
    }

    @PreAuthorize("hasAuthority('/ams/admin/delete')")
    @PostMapping("/{id:[0-9]+}/deleteById")
    public JsonResult<Void> deleteById(@PathVariable Long id){
        log.debug("开始处理【根据id删除管理员】的请求，参数：{}",id);
        adminService.deleteById(id);
        return JsonResult.ok();
    }

    @PreAuthorize("hasAuthority('/ams/admin/read')")
    @GetMapping("/{id:[0-9]+}/selectById")
    public JsonResult<Admin> selectById(@PathVariable Long id){
        log.debug("开始处理【根据id查询管理员】的请求，参数：{}",id);
        Admin admin= adminService.selectById(id);
        return JsonResult.ok(admin);
    }

    @PreAuthorize("hasAuthority('/ams/admin/read')")
    @GetMapping("/selectList")
    public JsonResult<List<Admin>> selectList(@ApiIgnore @AuthenticationPrincipal
                                                             LoginPrincipal loginPrincipal){
        log.debug("开始处理【查询管理员列表】的请求");
        List<Admin> adminList = adminService.selectList();
        return JsonResult.ok(adminList);
    }
    //@PreAuthorize("hasAuthority('/ams/admin/{id:[0-9]+}/update')")
    @ApiOperation("修改管理员")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> addNew(@PathVariable @Range(min = 1,message = "请提供有效Id") Long id,
                                   @Valid AdminUpdateDTO adminUpdateDTO){
        log.debug("开始处理【修改管理员】的请求，参数：{}", adminUpdateDTO);

        adminService.updateInfoById(id, adminUpdateDTO);
        return JsonResult.ok();
    }

}
